package com.ajx.blog.controller;

import com.ajx.blog.entity.CourseComment;
import com.ajx.blog.entity.CourseInfo;
import com.ajx.blog.service.api.CourseCommentService;
import com.ajx.blog.service.api.CourseInfoService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/course")
public class CourseController {

    // 日志
    private Logger logger = LoggerFactory.getLogger(BlogusersController.class);

    @Reference(url = "dubbo://localhost:20882")
    private CourseInfoService courseInfoService;

    @Reference(url = "dubbo://localhost:20882")
    private CourseCommentService courseCommentService;

    /**
     * 根据指定字段排序查询课程
     * 
     * @param model     数据
     * @param {Integer} page 当前页码数
     * @param {Integer} pageSize 每页记录数量
     * @param {String}  sortBy 排序字段
     * @param {String}  ordBy 正序/倒序
     * @return 页面路径
     */
    @RequestMapping("courseList")
    public String getCourseListByOrder(Model model,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "4") Integer pageSize,
            @RequestParam(value = "sortBy", required = false, defaultValue = "person_number") String sortBy,
            @RequestParam(value = "ordBy", required = false, defaultValue = "desc") String ordBy) {
        logger.info("根据指定字段排序查询课程");
        PageInfo<CourseInfo> pageInfo = courseInfoService.selectCourseListByOrder(page, pageSize, sortBy, ordBy);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("sortBy", sortBy);
        model.addAttribute("ordBy", ordBy);
        return "course";
    }

    /**
     * 去课程明细页面
     *
     * @param model 数据
     * @param cid   课程id
     * @return 页面路径
     */
    @RequestMapping("/doCourseDetail")
    public String doCourseDetail(Model model, Integer cid) {
        logger.info("根据id查询要报名的课程");
        CourseInfo courseInfo = courseInfoService.selectCourseById(cid);
        model.addAttribute("courseInfo", courseInfo);
        List<CourseComment> courseCommentList = courseCommentService.selectCourseCommentList(cid);
        model.addAttribute("courseCommentList", courseCommentList);
        return "course-details";
    }

    /**
     * 课程添加评论
     *
     * @param courseComment 添加的数据
     * @return 返回JSON数据
     */
    @RequestMapping("addCourseComment")
    @ResponseBody
    public Object addCourseComment(CourseComment courseComment) {
        logger.info("添加课程评论");
        Map<String, Object> map = new HashMap<>();
        if (courseCommentService.addCourseComment(courseComment)) {
            map.put("code", 0);
            map.put("msg", "评论成功");
            return map;
        } else {
            map.put("code", -1);
            map.put("msg", "评论失败");
            return map;
        }
    }

    /**
     * 课程报名
     *
     * @param cid 课程id
     * @return 返回JSON数据
     */
    @RequestMapping("updatePersonNumber")
    @ResponseBody
    public Object updatePersonNumber(Integer cid, @RequestParam("blogid") Integer userid) {
        logger.info("课程报名成功");
        if (courseCommentService.updatePersonNumber(cid, userid)) {
            return "0";
        }
        return "-1";
    }

}
